#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Hubs,  S2, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Sensor, S2,     ,               sensorI2CMuxController)
#pragma config(Sensor, S3,     seekerL,        sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S4,     seekerR,        sensorHiTechnicIRSeeker1200)
#pragma config(Motor,  mtr_S1_C1_1,     fl,            tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     fr,            tmotorTetrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     aux1,          tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     aux2,          tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S2_C1_1,     bl,            tmotorTetrix, PIDControl, encoder)
#pragma config(Motor,  mtr_S2_C1_2,     br,            tmotorTetrix, PIDControl, reversed, encoder)
#pragma config(Servo,  srvo_S2_C2_1,    grab1,                tServoStandard)
#pragma config(Servo,  srvo_S2_C2_2,    grab2,                tServoStandard)
#pragma config(Servo,  srvo_S2_C2_3,    rampl,                tServoContinuousRotation)
#pragma config(Servo,  srvo_S2_C2_4,    rampr,                tServoContinuousRotation)
#pragma config(Servo,  srvo_S2_C2_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S2_C2_6,    servo6,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

int Xcor;

void hold()
{
	motor[br] = 0;
	motor[bl] = 0;
	motor[fr] = 0;
	motor[fl] = 0;
	wait1Msec(1200);
}


void readX()
{
	if((SensorValue(seekerL) < 5) && (SensorValue(seekerR) < 5))
	{
		Xcor = 1;

	}

	if((SensorValue(seekerL) >= 5) && (SensorValue(seekerR) <= 5))
	{
		Xcor = 2;
	}

	if((SensorValue(seekerL) > 5) && (SensorValue(seekerR) > 5))
	{
		Xcor = 3;
	}

}

void rightSpeed(int power)
{
	motor[fr]=power;
	motor[br]=power;
	return;
}
void leftSpeed(int power)
{
	motor[fl]=power;
	motor[bl]=power;
	return;
}

void initalizeEncoders()
{
	nMotorEncoder[bl] = 0;
	nMotorEncoder[br] = 0;
	nMotorEncoder[fl] = 0;
	nMotorEncoder[fr] = 0;
	return;
}

task main()
{
	waitForStart();


	initalizeEncoders();

	nMotorEncoderTarget[bl] = 3750;       //forward
	nMotorEncoderTarget[br] = -3750;
	nMotorEncoderTarget[fl] = 3750;
	nMotorEncoderTarget[fr] = -3750;


	leftSpeed(50);
	rightSpeed(50);

	while(nMotorRunState[bl] != runStateIdle || nMotorRunState[br] != runStateIdle || nMotorRunState[fl] != runStateIdle || nMotorRunState[fr] != runStateIdle)
	{

	}

	hold();

	initalizeEncoders();



	nMotorEncoderTarget[bl] = -1150;    //turn
	nMotorEncoderTarget[br] = -1150;
	nMotorEncoderTarget[fl] = -1150;
	nMotorEncoderTarget[fr] = -1150;

	leftSpeed(-50);
	rightSpeed(50);

	while(nMotorRunState[bl] != runStateIdle || nMotorRunState[br] != runStateIdle || nMotorRunState[fl] != runStateIdle || nMotorRunState[fr] != runStateIdle)
	{

	}

  motor[fr] = 0;
	motor[br] = 0;
	motor[fl] = 0;
	motor[bl] = 0;
	motor[aux1] = -50;
	motor[aux2] = -50;
//	wait1Msec(300); bottom
	wait1Msec(1900);

	motor[aux1] = 0;
	motor[aux2] = 0;
	wait1Msec(300);


	hold();

	initalizeEncoders();

	nMotorEncoderTarget[bl] = 3600;
	nMotorEncoderTarget[br] = -3600;
	nMotorEncoderTarget[fl] = 3600;
	nMotorEncoderTarget[fr] = -3600;


	leftSpeed(50);
	rightSpeed(50);

	while(nMotorRunState[bl] != runStateIdle || nMotorRunState[br] != runStateIdle || nMotorRunState[fl] != runStateIdle || nMotorRunState[fr] != runStateIdle)
	{

	}

	hold();

	motor[aux1] = 50;
	motor[aux2] = 50;
	wait1Msec(300);

	motor[aux1] = 0;
	motor[aux2] = 0;
	wait1Msec(300);

	hold();

	nMotorEncoderTarget[bl] = -1440;
	nMotorEncoderTarget[br] = 1440;
	nMotorEncoderTarget[fl] = -1440;
	nMotorEncoderTarget[fr] = 1440;


	leftSpeed(-50);
	rightSpeed(-50);

	while(nMotorRunState[bl] != runStateIdle || nMotorRunState[br] != runStateIdle || nMotorRunState[fl] != runStateIdle || nMotorRunState[fr] != runStateIdle)
	{

	}





	readX();         //detect ir on grid

	hold();

	if(Xcor == 1)         // left side
	{
		PlaySound(soundDownwardTones);
	}

	if(Xcor == 2)        //  middle
	{
		PlaySound(soundUpwardTones);
	}

	if(Xcor == 3)       // right
	{
		PlaySound(soundBeepBeep);
	}


}
